/**
 * 
 * @param ele
 * @param inputname
 * @param dispname
 * @param parent
 * @param opts
 */
if(!HuploadifyHelper2){
	function HuploadifyHelper2(ele,inputname,dispname,parent,opts){
		this.$ele = $(ele);
		this.$parent = $(parent);
		if($(inputname,ele).length>0){
			this.$input = $(inputname,ele);
		}else{
			this.$input = $(inputname,parent);
		}
		if($(dispname,ele).length>0){
			this.$disp = $(dispname,ele);
		}else{
			this.$disp = $(dispname,parent);
		}
		this.opts = opts;
	}
	/**
	 * 上传成功操作
	 * @param file
	 * @param data
	 */
	HuploadifyHelper2.prototype.onUploadSuccess= function(file,data){
		var _this = this;
	 	data=$.parseJSON(data);
	    if(data.success){
	 	   if(data.obj!=null){
	 		   for(var i=0;i<data.obj.length;i++){
	 			    var html="";
					html+="<li attachfileid=\""+data.obj[i].path+"\"><a href=\""+basePath+"/upload/downloadAttach/"+data.obj[i].id+"\">"+data.obj[i].name+"</a>&nbsp;&nbsp;&nbsp;" 
						+"<a name=\"delete\" href=\"javascript:;\" style=\"color:red\">删除</a>" 
						+"</li>";					
	     		   	   //修改附件id
					//alert(html);
					var pre = this.$input.val();
					var now="";
					if(pre){
						now = pre+","+data.obj[i].url;
					}else{
						now = data.obj[i].url
					}
					this.$input.val(now);
					this.$disp.append(html);				
					this.$disp.find("a[name='delete']:last").click(function(){
						var attachId = $(this).parent().attr("attachfileid");
						_this.attachfileDelete(this,attachId);
					});
				}             		   	  
	 	   }
	     }
	 
	};

	/**
	 * 文件上传出错
	 */
	HuploadifyHelper2.prototype.onUploadError = function(file,response){ 
	 	//alert("上传失败");
	};
	/**
	 * 删除上传附件
	 * @param obj
	 */
	HuploadifyHelper2.prototype.attachfileDelete = function(obj,url){
		var $input = this.$input;
		var $disp = this.$disp;
		$.ajax({
			url:basePath+'/upload/attach/delete3/'+url,
			type:'POST',
			dataType:'json',
			success:function(data){
				if(data.success&&data.obj){
					var pre = $input.val();
					if(pre){
						pre = pre.replace(data.obj+",",""); 
						pre = pre.replace(data.obj,"");
						// 1,2,3,移除3最后一个留下的","
						if(pre.charAt(pre.length-1)==","){
							pre= pre.substring(0,pre.length-1);
						}  										  					
					}
					$input.val(pre);
					$(obj,$disp).parent().remove();//删除
				}
			},
			error:function(XMLHttpRequest,textStatus, errorThrown){
			}
		});
	};

	HuploadifyHelper2.prototype.init = function(){
		var _this = this;
		var opts={
				showUploadedPercent:true,//上传百分比
	            showUploadedSize:true,//实时显示上传文件大小
				uploader:basePath+'/upload/upload',
				onUploadSuccess:function(file,data){
					_this.onUploadSuccess(file,data);
				},
	            onUploadError:function(file,response){
	            	_this.onUploadError(file,response);
	            }
			};
		$.extend(opts,_this.opts);
		$(_this.$ele).Huploadify(opts);
	};
}




